'
• A detailed and elaborated answer to the task question, if there is any.
"PLEASE SHOW ALL OUTPUT FOR ALL TASKS PROGRAMS""PLEASE SHOW ALL OUTPUT FOR ALL TASKS PROGRAMS"
divide.c
-------------
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "mpi.h"
int main(int argc, char * argv[] ) {
int rank; /* rank of each MPI process */
int size; /* total number of MPI processes */
int i; /* counter */
int distance; /* distance between sender and receiver */
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
/* Am I sender or receiver? */
/* Who am I sending/receiving to/from */
distance = 1;
i = 1;
while (distance <= size / 2){
if (rank < distance) {
printf ("At time step %d, sender %d sends to %d\\n", i, rank, rank + distance);
}
if ((rank >= distance) && (rank < distance * 2)){
printf ("At time step %d, receiver %d receives from %d\\n", i, rank, rank - distance);
}
printf ("Process %d has distance value %d and time step %d\\n", rank, distance, i);
distance = distance * 2;
i += 1;
}
MPI_Finalize();
return 0;
}
scatterv.c
--------------
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int rank, size;
int i;
int sendcounts[4] = {1,2,3,4}; /* each process will receive its rank plus 1 numbers from the sendbuf array */
int displs[4] = {0,0,0,0}; /* array describing the displacements where each segment begins and is initialized to all 0s */
int sendbuf[10] = {2,13,4,3,5,1,0,12,10,8}; /* the buffer to be sent */
int *recvbuf; /* array at each process to receive data. To be initialized based on process rank */
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
/* initializes recvbuf to contain exactly rank plus 1 numbers */
recvbuf = malloc(sizeof(int)* (rank + 1));
// calculate displacements
for (i = 1; i < 4; i++) {
displs[i] = displs[i-1] + sendcounts[i-1];
}
// divide the data among processes as described by sendcounts and displs
MPI_Scatterv(sendbuf, sendcounts, displs, MPI_INT, recvbuf, (rank + 1), MPI_INT, 0, MPI_COMM_WORLD);
// print what each process received
printf("%d: ", rank);
for (i = 0; i < sendcounts[rank]; i++) {
printf("%d ", recvbuf[i]);
}
printf("\\n");
free(recvbuf);
MPI_Finalize();
return 0;
}
gatherv.c
--------------
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int rank, size;
int i;
int recvcounts[4] = {1,2,3,4}; /* process 0 will receive from each process that process rank */
/* plus 1 numbers */
int displs[4] = {0,0,0,0}; /* array describing the displacements where each segment begins and is initialized to all 0s */
int *sendbuf; /* the buffer to be sent. will be initialized individually at each process */
int *recvbuf; /* arrayto receive data. will only be initialized at process 0*/
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
/* initializes recvbuf to receive 10 numbers */
if (rank == 0){
recvbuf = malloc(sizeof(int) * (10));
for (i = 0; i < 10; i ++)
recvbuf[i] = -1;
}
/* initializes sendbuf to receive 10 numbers */
sendbuf = malloc(sizeof(int) * (rank + 1));
for (i = 0; i < (rank + 1); i++){
sendbuf[i] = rank;
}
// calculate displacements
for (i = 1; i < 4; i++) {
displs[i] = displs[i-1] + recvcounts[i-1];
}
// divide the data among processes as described by sendcounts and displs
MPI_Gatherv(sendbuf, rank + 1, MPI_INT, recvbuf, recvcounts, displs, MPI_INT, 0, MPI_COMM_WORLD);
// print what process has at the end
if (rank == 0){
for (i = 0; i < 10; i++) {
printf("%d ", recvbuf[i]);
}
printf("\\n");
free(recvbuf);
}
MPI_Finalize();
return 0;
}
gatherv.c
--------------
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int rank, size;
int i;
int recvcounts[4] = {1,2,3,4}; /* process 0 will receive from each process that process rank */
/* plus 1 numbers */
int displs[4] = {0,0,0,0}; /* array describing the displacements where each segment begins and is initialized to all 0s */
int *sendbuf; /* the buffer to be sent. will be initialized individually at each process */
int *recvbuf; /* arrayto receive data. will only be initialized at process 0*/
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
/* initializes recvbuf to receive 10 numbers */
if (rank == 0){
recvbuf = malloc(sizeof(int) * (10));
for (i = 0; i < 10; i ++)
recvbuf[i] = -1;
}
/* initializes sendbuf to receive 10 numbers */
sendbuf = malloc(sizeof(int) * (rank + 1));
for (i = 0; i < (rank + 1); i++){
sendbuf[i] = rank;
}
// calculate displacements
for (i = 1; i < 4; i++) {
displs[i] = displs[i-1] + recvcounts[i-1];
}
// divide the data among processes as described by sendcounts and displs
MPI_Gatherv(sendbuf, rank + 1, MPI_INT, recvbuf, recvcounts, displs, MPI_INT, 0, MPI_COMM_WORLD);
// print what process has at the end
if (rank == 0){
for (i = 0; i < 10; i++) {
printf("%d ", recvbuf[i]);
}
printf("\\n");
free(recvbuf);
}
MPI_Finalize();
return 0;
}
------------------------------------------------------------------
"PLEASE SHOW ALL OUTPUT FOR ALL TASKS PROGRAMS"
NOTE:
• A detailed and elaborated answer to the task question, if there is any.